import Toast from '@vant/weapp/toast/toast';
import {updateFamilyInfo} from "../../api/userInfo"

Page({

  /**
   * 页面的初始数据
   */
  data: {
    populationSize:3,
    familyInfoArr:[]
  },

  // 同步人口数量的变化
  onChange(e){
    this.setData({
      populationSize:e.detail
    })
  },

  // 保存用户填写信息(统一获取子组件中的数据)
  async savaInfo(){
    let {familyInfoArr,populationSize} = this.data
    let flag = false // 立即结束当前循环的标志

    for(let i = 0;i < populationSize - 1;i++ ){
      // 获取子组件中的每一条家人信息
      const {afamilyInfo} = this.selectComponent("#table-"+i)

      if(!flag){
        let currentAfamilyInfoIsOk = true

        for(let index in afamilyInfo){
          // console.log(afamilyInfo[index])
          // 当信息中有属性为空值时，停止检测afamilyInfo的其它属性，
          // 同时将flag置为true，不再遍历其它子组件中的数据
          if(afamilyInfo[index].length == 0){
            currentAfamilyInfoIsOk = false
            flag = true
            Toast({
              message:"请完成所有输入后再提交"
            })
            break
          }
        }

        if(currentAfamilyInfoIsOk){
          familyInfoArr.push(afamilyInfo)
        }
      }else{
        break
      }
    }

    // this.setData({
    //   familyInfoArr
    // })

    if(familyInfoArr.length === populationSize - 1){
      let ret = await updateFamilyInfo(this.data)
      if(ret.code === 200){
        Toast({
          type:"success",
          message:"提交成功"
        })
      }
    }
  }
})